//-----------------------------------------------------------------------------
//----- WindoTypeDlg.cpp : implementation file
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"
#include "WindoTypeDlg.h"
#include "geassign.h"

extern CWindoTypeDlg* gpDlg;

//-----------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (CWindoTypeDlg, CAcUiDialog)

BEGIN_MESSAGE_MAP(CWindoTypeDlg, CAcUiDialog)
	//{{AFX_MSG_MAP(CWindoTypeDlg)
	ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)    // Needed for modeless dialog.
	ON_EN_KILLFOCUS(IDC_EDIT_HEIGHT, OnKillfocusEditHeight)
	ON_EN_KILLFOCUS(IDC_EDIT_WIDTH, OnKillfocusEditWidth)
	ON_BN_CLICKED(IDC_BTN_PICKPT, OnBtnPickPt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
CWindoTypeDlg::CWindoTypeDlg (CWnd* pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (CWindoTypeDlg::IDD, pParent, hInstance) {
	//{{AFX_DATA_INIT(CWindoTypeDlg)
	m_nCols = windInfo.m_nCols;
	m_nRows = windInfo.m_nRows;
	m_nWindType = -1;
	//}}AFX_DATA_INIT
}

void CWindoTypeDlg::DoDataExchange (CDataExchange *pDX) {
	CAcUiDialog::DoDataExchange (pDX) ;
	//{{AFX_DATA_MAP(CWindoTypeDlg)
	DDX_Control(pDX, IDC_EDIT_HEIGHT, m_ctrlEdHeight);
	DDX_Control(pDX, IDC_EDIT_WIDTH, m_ctrlEdWidth);
	DDX_Control(pDX, IDC_EDIT_XVAL, m_ctrlEdXVal);
	DDX_Control(pDX, IDC_EDIT_YVAL, m_ctrlEdYVal);
	DDX_Text(pDX, IDC_EDIT_COLS, m_nCols);
	DDV_MinMaxInt(pDX, m_nCols, 1, 10);
	DDX_Text(pDX, IDC_EDIT_ROWS, m_nRows);
	DDV_MinMaxInt(pDX, m_nRows, 1, 10);
	DDX_Radio(pDX, IDC_RB_TYPERECT, m_nWindType);
	DDX_Control(pDX, IDC_BTN_PICKPT, m_ctrlBtnPickPt);
	//}}AFX_DATA_MAP
}

// Needed for modeless dialogs to keep focus.
// Return FALSE to not keep the focus, return TRUE to keep the focus
LONG CWindoTypeDlg::OnAcadKeepFocus(UINT, UINT)
{
  return TRUE;
}

//-----------------------------------------------------------------------------

void CWindoTypeDlg::OnOK() 
{
	// TODO: Add extra validation here

	CString strMsg1, strMsg2, strMsg;

	if (!UpdateData(TRUE))
	{
		return;
	}
	
	updateGlobalDocData();

	strMsg1 = "If you were to draw a window, then\n\n";

	strMsg2.Format("Type\t = %s\nWidth\t = %.2lf\nHeight\t = %.2lf\nRows\t = %d\nCols\t = %d\nX\t = %.2lf\nY\t = %.2lf",
				  windInfo.windType,
				  windInfo.m_dWindWt,
				  windInfo.m_dWindHt,
				  windInfo.m_nRows,
				  windInfo.m_nCols,
				  windInfo.m_dXVal,
				  windInfo.m_dYVal);

	strMsg = strMsg1 + strMsg2;

	CWnd::MessageBox(strMsg, "Selected Window Parameters", MB_ICONINFORMATION);

}

BOOL CWindoTypeDlg::OnInitDialog() 
{
	CAcUiDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString str_edboxVal;
	CString fName;
	CString mainTitle("Window Parameters Modeless - ");
    AcApDocument *pCurrDoc = acDocManager->curDocument();
	fName = pCurrDoc->docTitle();
	mainTitle += fName;

	this->SetWindowText(mainTitle);


	CButton *pWindoType;
	CString strWindType(windInfo.windType);
	if(strWindType == "Rect")
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPERECT);
		pWindoType->SetCheck(1);
	}
	else if(strWindType == "Arch")
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPEARCH);
		pWindoType->SetCheck(1);
	}
	else	// It must be and "Apex" type of window
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPEAPEX);
		pWindoType->SetCheck(1);
	}

	m_ctrlEdHeight.SetRange(10.00, 120.00);
	m_ctrlEdWidth.SetRange(10.00, 120.00);

	acdbRToS(windInfo.m_dWindHt, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdHeight.SetWindowText(str_edboxVal);

	acdbRToS(windInfo.m_dWindWt, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdWidth.SetWindowText(str_edboxVal);

	acdbRToS(windInfo.m_dXVal, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdXVal.SetWindowText(str_edboxVal);

	acdbRToS(windInfo.m_dYVal, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdYVal.SetWindowText(str_edboxVal);
	
	m_ctrlBtnPickPt.AutoLoad();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWindoTypeDlg::OnKillfocusEditHeight() 
{
	// TODO: Add your control notification handler code here
	m_ctrlEdHeight.Convert();
		if(!m_ctrlEdHeight.Validate())
	{
		::AfxMessageBox("Sorry, Range = 10.00 to 120.00");
		m_ctrlEdHeight.SetFocus();
		m_ctrlEdHeight.SetSel(0, -1);
	}	
}

void CWindoTypeDlg::OnKillfocusEditWidth() 
{
	// TODO: Add your control notification handler code here
	m_ctrlEdWidth.Convert();
	if(!m_ctrlEdWidth.Validate())
	{
		::AfxMessageBox("Sorry, Range = 10.00 to 120.00");
		m_ctrlEdWidth.SetFocus();
		m_ctrlEdWidth.SetSel(0, -1);
	}	
}

void CWindoTypeDlg::OnBtnPickPt() 
{
	// TODO: Add your control notification handler code here
	// Here we hide our modeless dialog
	// to allow the user to pick a point
	AcGePoint3d pkPt;
	int retCode;

	CString str_edboxVal;

	UpdateData(TRUE);
	updateGlobalDocData();

	//ShowWindow(SW_HIDE);	// Hide our dialog
	BeginEditorCommand();

	acedInitGet(NULL, NULL);

	retCode = acedGetPoint(NULL, "\nPick lower left corner of window: ",
							asDblArray(pkPt));

	switch(retCode)
	{
		case RTCAN  :
		case RTNONE :

			pkPt.set(0.0, 0.0, 0.0);

		break;

		case RTNORM :
		break;

		case RTERROR :
		break;
	}

	windInfo.m_dXVal = pkPt.x;
	windInfo.m_dYVal = pkPt.y;

	CompleteEditorCommand(); // This replaces the next commented out lines

	//ShowWindow(SW_SHOW); // Display our dialog again


	// Update our X and Y Edit Boxes
	acdbRToS(windInfo.m_dXVal, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdXVal.SetWindowText(str_edboxVal);

	acdbRToS(windInfo.m_dYVal, 2, 3, str_edboxVal.GetBuffer(80));
	m_ctrlEdYVal.SetWindowText(str_edboxVal);

}

void CWindoTypeDlg::updateGlobalDocData()
{
	m_ctrlEdHeight.GetWindowText(m_strHeight);
	m_ctrlEdWidth.GetWindowText(m_strWidth);
	m_ctrlEdXVal.GetWindowText(m_strXVal);
	m_ctrlEdYVal.GetWindowText(m_strYVal);

	windInfo.m_dWindWt  = atof(m_strWidth);
	windInfo.m_dWindHt	= atof(m_strHeight);
	windInfo.m_dXVal	= atof(m_strXVal);
	windInfo.m_dYVal	= atof(m_strYVal);
	windInfo.m_nCols	= m_nCols;
	windInfo.m_nRows	= m_nRows;

	switch (m_nWindType)
	{
		case 0 :
			strcpy(windInfo.windType, "Rect");
		break;

		case 1 :
			strcpy(windInfo.windType, "Arch");
		break;

		case 2 :
			strcpy(windInfo.windType, "Apex");
		break;
	}
}

void CWindoTypeDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	// Do NOT call CAcUiDialog::OnCancel(); 
	// Remember we're modeless,
	// Call DestroyWindow() instead!
	
	DestroyWindow();
}

void CWindoTypeDlg::PostNcDestroy() 
{
	// TODO: Add your specialized code here and/or call the base class
	delete this;

	if(gpDlg != NULL)
	{
		gpDlg = NULL;
	}
	
	CAcUiDialog::PostNcDestroy();
}
